import { topCategory } from "@/api/constants"
import {findAllCategory} from '@/api/category'

export default {
  namespaced:true,
  state () {
    return {
      // 商品分类集合
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations:{
    setList (state,payload) {
      state.list = payload
    },
    // 修改当前一级分类下的open数据为true
    show (state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = true
    },
    // 修改当前一级分类下的open数据为false
    hide (state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = false
    }
  },
  actions: {
    async getList ({commit}) {
      const { result } = await findAllCategory()
      result.forEach(item => {
        item.open = false
      })
      // 获取数据成功，提交mutations进行数据修改
      commit('setList', result)
    }
  }
}